/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cannes.dao.implement;

import cannes.bean.vip.Action;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 *
 * @author p1008977
 */
public class ActionDAO extends DAO<Action>{

    public ActionDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Action obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "INSERT INTO action VALUES( " + obj.getVipId() + "," + obj.getId() +"," + obj.getDate() + "," + obj.getType() + "," + obj.getDescription());
        return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Action obj) {
        try
        {
        this.connect.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(
                "UPDATE action SET vipid= " + obj.getVipId() + ",actionid =" + obj.getId() +",dateaction = " + obj.getDate() + ",typeaction" + obj.getType()+",contenu = " + obj.getDescription());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return false;
    }

    @Override
    public boolean delete(Action obj) {
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "DELETE FROM vip WHERE vip = " + obj.getId());
            result.rowDeleted();

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public int next() {
        int i = 0;
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT max(actionid) from action");
            if (result.first()) i = result.getInt(1)+1;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;        
    }



    @Override
    public Action find(int id) {
        Action action = new Action();

        try {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM action WHERE actionid = " + id);
            if (res.first()) {
                action = new Action(res.getInt("vipid"), res.getInt("actionid"), res.getDate("dateaction"),
                        res.getString("typeaction"),res.getString("description"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return action;

    }

    @Override
    public ArrayList<Action> findAll() {
        ArrayList<Action> list = new ArrayList<Action>();
        Action action = new Action();

        try
        {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM action");
            while (res.next())
                action = new Action(res.getInt("vipid"), res.getInt("actionid"), res.getDate("dateaction"),
                        res.getString("typeaction"),res.getString("description"));
                list.add(action);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public ArrayList<Action> findByVipId(int id) {
        ArrayList<Action> list = new ArrayList<Action>();
        Action action = new Action();

        try
        {
            ResultSet res = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM action WHERE vipid = "+id);
            while (res.next())
                action = new Action(res.getInt("vipid"), res.getInt("actionid"), res.getDate("dateaction"),
                        res.getString("typeaction"),res.getString("description"));
                list.add(action);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }    
    
}
